package vn.edu.saigontech.ncms.web.user;

import vn.edu.saigontech.ncms.model.user.User;
import vn.edu.saigontech.ncms.service.RoleService;
import vn.edu.saigontech.ncms.service.SiteService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class UserAction extends BaseAction {
	
	private Long id;
	private String username;
	private String password;
	private String firstName;
	private String lastName;
	private boolean active;
	private String email;
	private Long[] roles;
	private Long[] sites;
	private String comment;
	
	public String showAdd() {
		return SUCCESS;
	}
	
	public String add() {
		User user = new User();
		user.setUsername(this.username);
		user.setPassword(this.password);
		user.setFirstName(this.firstName);
		user.setLastName(this.lastName);
		user.setEmail(this.email);
		if(this.sites != null && this.sites[0] != null) {
			user.setSites(SiteService.INSTANCE.find(this.sites));
		}
		else {
			user.setSites(null);
		}
		user.setRoles(RoleService.INSTANCE.find(this.roles));
		user.setComment(this.comment);
		user.setActive(true);
		user = UserService.INSTANCE.persist(user);
		this.id = user.getId();
		this.addLoggingDetail(BaseAction.ADD, "logging.user.add", user.getUsername());
		return SUCCESS;
	}
	
	public String showEdit() {
		REQUEST().setAttribute("model", UserService.INSTANCE.findById(this.id));
		return SUCCESS;
	}
	
	public String edit() {
		User user = UserService.INSTANCE.findById(this.id);
		user.setUsername(username);
		if(!Checker.isEmpty(this.password)) {
			user.setPassword(this.password);
		}
		user.setFirstName(firstName);
		user.setLastName(lastName);
		user.setEmail(email);
		user.setActive(active);
		if(this.sites != null && this.sites[0] != null) {
			user.setSites(SiteService.INSTANCE.find(this.sites));
		}
		else {
			user.setSites(null);
		}
		user.setRoles(RoleService.INSTANCE.find(this.roles));
		user.setComment(comment);
		user = UserService.INSTANCE.persist(user);
		this.addLoggingDetail(BaseAction.EDIT, "logging.user.edit", user.getUsername());
		return SUCCESS;
	}
	
	public String delete() {
		User user = UserService.INSTANCE.findById(this.id);
		if(UserService.INSTANCE.deleteById(this.id)) {
			this.setKeyMessage("confirm.deleteSuccessfully", "user");
			this.addLoggingDetail(BaseAction.DELETE, "logging.user.delete", user.getUsername());
		}
		else {
			this.setKeyMessage("confirm.deleteUnsuccessfully", "user");
		}
		return SUCCESS;
	}
	
	public String list() {
		REQUEST().setAttribute("users", UserService.INSTANCE.findAll());
		return SUCCESS;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	public Long[] getRoles() {
		return roles;
	}

	public void setRoles(Long[] roles) {
		this.roles = roles;
	}

	public Long[] getSites() {
		return sites;
	}

	public void setSites(Long[] sites) {
		this.sites = sites;
	}
}